package edu.chinasoft.servlet;

import edu.chinasoft.domain.Dept;
import edu.chinasoft.domain.Idcard;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 
 * @author 
 * @date 2020年1月20日:上午9:42:58
 * @version V1.0
 * @Description: 
 * 
*/
@WebServlet(name="UpdateUserServlet",value="/servlet/updateuserservlet")
public class  UpdateUserServlet  extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(UpdateUserServlet.class);
	private UserService userService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info(" UpdateUserServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		UserService userServiceImp = wac.getBean("userService", UserService.class);
		this.userService = userServiceImp;
		
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String user_password = request.getParameter("user_password");
		String user_idcard = request.getParameter("user_idcard");
		String select_dept = request.getParameter("select_dept");
		String user_desc = request.getParameter("user_desc");
		String user_status = request.getParameter("user_status");
		User user = new User(user_name,user_password,user_desc,user_status);
		user.setUserId(Integer.parseInt(user_id));
		Idcard idcard = new Idcard();
		idcard.setIdcardNo(user_idcard);
		user.setIdcard(idcard);
		Dept dept = new Dept();
		dept.setDeptName(select_dept);
		user.setDept(dept);
		logger.info("身份证号为："+user_idcard);
		logger.info("部门为："+select_dept);
		logger.info("用户为："+user);

		boolean result = userService.updateUserByUserId(user);
		if(result) {//更新成功
			response.sendRedirect(""+request.getContextPath()+"/servlet/showuserservlet");
		}else {//更新失败
			response.sendRedirect(""+request.getContextPath()+"/updateusererror.jsp");
		}
			
		
		logger.info(" UpdateUserServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info(" UpdateUserServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.info(" UpdateUserServlet >doPost end.......");
	}
	
}
